package com.stars.services.impl.system;/**
 * Created by luodajie on 2019/1/5.
 */

import com.stars.common.base.BaseCode;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.BaseUUID;
import com.stars.common.base.ResultMsg;
import com.stars.dao.mapper.system.SystemPermissionMapper;
import com.stars.dao.mapper.system.SystemPermissionRelationMapper;
import com.stars.dao.mapper.system.SystemUserRelationMapper;
import com.stars.dao.mapper.system.SystemUserRoleMapper;
import com.stars.entity.dto.sys.SystemPermissionDTO;
import com.stars.entity.sys.SystemPermission;
import com.stars.entity.sys.SystemPermissionRelation;
import com.stars.entity.sys.SystemUserRole;
import com.stars.services.impl.BaseConfigServiceImpl;
import com.stars.services.system.UserRoleConfigService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :dj
 * @version : 1.0.0
 * @description
 * @Date 2019-01-05 12:39
 * @note
 */
@Service
public class UserRoleConfigServiceImpl extends BaseConfigServiceImpl<SystemUserRoleMapper, SystemUserRole> implements UserRoleConfigService {

    @Autowired
    private SystemUserRoleMapper systemUserRoleMapper;
    @Autowired
    private SystemUserRelationMapper systemUserRelationMapper;
    @Autowired
    private SystemPermissionRelationMapper systemPermissionRelationMapper;
    @Autowired
    private SystemPermissionMapper systemPermissionMapper;
    @Override
    public ResultMsg roleInsert(SystemUserRole systemUserRole) throws Exception {
        if(StringUtils.isBlank(systemUserRole.getId())){
            systemUserRole.setId(BaseUUID.getUUID());
        }
        if(StringUtils.isBlank(systemUserRole.getPid())){
            systemUserRole.setPid(BaseConstant.BASE_ROOT);
        }
        //1，查询角色名称是否存在
        List<SystemUserRole> list = systemUserRoleMapper.findByRoleName(systemUserRole.getRoleName());
        if(list != null && list.size()>0){
            return ResultMsg.failure("系统已经存在该角色！");
        }
        systemUserRoleMapper.insert(systemUserRole);

        return ResultMsg.success();
    }

    @Override
    public ResultMsg roleUpdate(SystemUserRole systemUserRole, String menus) throws Exception {
        //1检验角色是否存在
        String id =systemUserRole.getId();
        SystemUserRole byEntityId = systemUserRoleMapper.findById(id);
        if (null == byEntityId) {
            return  ResultMsg.customMsg(BaseCode.VLIDATE_PARM, "角色不存在,无法修改!", null);
        }
        //2，查询角色名称是否存在
        List<SystemUserRole> list = systemUserRoleMapper.findByRoleNameNotId(systemUserRole.getRoleName(),systemUserRole.getId());
        if(list != null && list.size()>0){
            return ResultMsg.failure("系统已经存在该角色！");
        }
        systemUserRoleMapper.updateByPrimaryKeySelective(systemUserRole);
        return  ResultMsg.success();
    }



    @Override
    public ResultMsg deleteEntity(String id) throws Exception {
        /**此id为角色id **/
        /**  根据角色id查询与其关联的表:system_permission_relation,system_user_relation **/
        //删除与其关联的用户关系
        systemUserRelationMapper.deleteByRoleId(id);
        //删除与其关联的资源关系
        systemPermissionRelationMapper.deleteByRoleId(id);
        //最后删除角色
        systemUserRoleMapper.deleteByPrimaryKey(id);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg updatePermission(String id, String menuIds) throws Exception {
        //3，修改角色资源关系
        //修改资源id
        if(StringUtils.isNotBlank(menuIds)){
            String [] menuList =menuIds.split(BaseConstant.SPOT);
            //原来角色拥有资源
            SystemPermissionDTO dto = new SystemPermissionDTO();
            dto.setRoleId(id);
            List<SystemPermissionDTO> permissionList = systemPermissionMapper.findByRoleId(dto);
            //如果找不到资源，删除对应关系
            for(SystemPermissionDTO p:permissionList){
                boolean isDel = true;
                for(String mid:menuList){
                    if(p.getId().equals(mid)){
                        isDel = false;
                        break;
                    }
                }
                if(isDel) {
                    SystemPermissionRelation systemPermissionRelation = new SystemPermissionRelation();
                    systemPermissionRelation.setPerId(p.getId());
                    systemPermissionRelation.setPerRoleId(id);
                    systemPermissionRelationMapper.deleteByRoleIdAndPerId(systemPermissionRelation);
                }
            }
            //如果是添加
            for(String mid:menuList){
                boolean isAdd =true;
                for(SystemPermissionDTO p:permissionList){
                    if(p.getId().equals(mid)){
                        isAdd = false;
                        break;
                    }
                }
                if(isAdd) {
                    SystemPermissionRelation systemPermissionRelation = new SystemPermissionRelation();
                    systemPermissionRelation.setPerId(mid);
                    systemPermissionRelation.setPerRoleId(id);
                    systemPermissionRelationMapper.insert(systemPermissionRelation);
                }
            }
        }

        return ResultMsg.success();
    }
}
